package cn.edu.xcu.ckl.fragment;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.lzy.okgo.OkGo;

import cn.edu.xcu.ckl.ProductDetailsActivity;
import cn.edu.xcu.ckl.R;
import cn.edu.xcu.ckl.adapter.LeftListAdapter;
import cn.edu.xcu.ckl.adapter.RightListAdapter;
import cn.edu.xcu.ckl.api.ApiConstants;
import cn.edu.xcu.ckl.base.BaseFragment;
import cn.edu.xcu.ckl.entity.ProductInfo;
import cn.edu.xcu.ckl.entity.ProductListInfo;
import cn.edu.xcu.ckl.http.HttpStringCallback;
import cn.edu.xcu.ckl.utils.AcListener;
import cn.edu.xcu.ckl.utils.GsonUtils;


public class CategoryFragment extends BaseFragment {

    private RecyclerView leftRecyclerView;
    private RecyclerView rightRecyclerView;
    private LeftListAdapter mLeftListAdapter;
    private RightListAdapter mRightListAdapter;

    private String product_title;


    private AcListener mAcListener;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mAcListener = (AcListener) context;
    }
    @Override
    protected int getContentView() {
        return R.layout.fragment_category;
    }

    @Override
    protected void initView() {
        //初始化控件
        leftRecyclerView = rootView.findViewById(R.id.leftRecyclerView);
        rightRecyclerView = rootView.findViewById(R.id.rightRecyclerView);

    }

    @Override
    protected void setListener() {
        //初始化适配器
        mLeftListAdapter = new LeftListAdapter();
        //设置适配器
        leftRecyclerView.setAdapter(mLeftListAdapter);

        mRightListAdapter = new RightListAdapter(getActivity());
        rightRecyclerView.setAdapter(mRightListAdapter);
        //recyclerView点击事件
        mRightListAdapter.setOnItemClickListener(new RightListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(ProductInfo productInfo, int position) {

                //跳转传值
                Intent intent = new Intent(getActivity(), ProductDetailsActivity.class);
                //intent 传递对象的时候，实体类一定要 implements Serializable
                intent.putExtra("productInfo", productInfo);
                startActivityForResult(intent,2000);

            }
        });


        //recyclerView点击事件
        mLeftListAdapter.setLeftListOnClickItemListener(new LeftListAdapter.LeftListOnClickItemListener() {
            @Override
            public void onItemClick(ProductInfo productInfo, int position) {
                mLeftListAdapter.setCurrentIndex(position);

                //点击左侧分类切换对应的列表数据
                queryProductList(productInfo.getProduct_title());
            }
        });

    }

    @Override
    protected void initData() {
        //获取商品分类
        queryProductTypeList();
    }


    /**
     * 获取商品分类
     */
    private void queryProductTypeList() {

        OkGo.<String>get(ApiConstants.QUERY_PRODUCT_TYPE_LIST_URL).execute(new HttpStringCallback(null) {
            @Override
            protected void onSuccess(String msg, String response) {
                ProductListInfo productListInfo = GsonUtils.parseJson(response, ProductListInfo.class);
                if (null != mLeftListAdapter) {
                    mLeftListAdapter.setDataList(productListInfo.getList());
                }
                if (productListInfo.getList().size() > 0) {
                    product_title = productListInfo.getList().get(0).getProduct_title();
                    //获取对应的商品
                    queryProductList(product_title);

                }
            }

            @Override
            protected void onError(String response) {

            }
        });

    }


    /**
     * 获取分类商品数据
     */
    private void queryProductList(String product_type) {

        OkGo.<String>get(ApiConstants.QUERY_PRODUCT_LIST_URL)
                .params("product_type",product_type)
                .execute(new HttpStringCallback(getActivity()) {
                    @Override
                    protected void onSuccess(String msg, String response) {
                        ProductListInfo productListInfo = GsonUtils.parseJson(response, ProductListInfo.class);
                        if (null != mRightListAdapter) {
                            mRightListAdapter.setListData(productListInfo.getList());
                        }
                    }

                    @Override
                    protected void onError(String response) {

                    }
                });

    }

    public void loadData() {
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==2000){
            if (null!=mAcListener){
                mAcListener.currentIndex(2);
            }
        }
    }
}